/*
Copyright 2010-2011 CumulusCloud
http://cumuluscloud.cc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cumulusCloud.knowledge;

import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

import cumulusCloud.common.InformationPath;

public class PathKnow {
	static private PathKnow instance;
	
	public static PathKnow getInstance() {
		if (instance == null) instance = new PathKnow();
		return instance;
	}
	
	private String catalinaPath;
	private String javaPath;
	
	private PathKnow() {
		Logger logger = java.util.logging.Logger.getLogger(InformationPath.class.getName());
		try {
			File newPath = File.createTempFile("ccSDK-", "");
			newPath.delete();
			newPath.mkdirs();
			catalinaPath = newPath.getAbsolutePath();
		} catch (IOException e) {
			e.printStackTrace();
		}
		javaPath = System.getProperty("java.home");
		
		if (catalinaPath == null) {
			logger.severe("Could not get the Catalina Path");
		}
		
		if (javaPath == null) {
			logger.severe("Could not get the Java Path");
		}
		
		String paths = "getCatalinaConfigPath: " + getCatalinaConfigPath() + "\n";
		paths += "getCloudAppsDataPath: " + getCloudAppsDataPath("") + "\n";
		paths += "getCloudAppsInstallPath: " + getCloudAppsInstallPath() + "\n";
		paths += "getCloudGlobalConfigPath: " + getCloudGlobalConfigPath() + "\n";
		paths += "getJavaBinPath: " + getJavaBinPath() + "\n";
		logger.info(paths);
		
		
	}

	private void createDir(String path) {
		File f = new File(path);
		if (!(f.exists())) {
			f.mkdirs();
		}
	}
	
	public String getCatalinaConfigPath() {
		return catalinaPath + File.separatorChar + "conf" + File.separatorChar;
	}
	
	public String getCloudAppsDataPath(String appName) {
		String path = catalinaPath + File.separatorChar + "cloud" + File.separatorChar + "appdata" + File.separatorChar + appName + File.separatorChar;
		createDir(path);
		return path;
	}
	
	public String getCloudAppsInstallPath() {
		String path = catalinaPath + File.separatorChar + "webapps" + File.separatorChar;
		createDir(path);
		return path;
	}
	
	public String getCloudGlobalConfigPath() {
		String path = catalinaPath + File.separatorChar + "cloud" + File.separatorChar + "config" + File.separatorChar;
		createDir(path);
		return path;
	}
	
	public String getJavaBinPath() {
		return javaPath + File.separatorChar + "bin" + File.separatorChar;
	}
}
